﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses

Public Class EditHotel
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'If Request.Cookies("Admin") Is Nothing Then
            If Session.Item("UserID") Is Nothing Then
                'Response.Redirect("Login.aspx")
            End If
            If Not IsPostBack = True Then
                Fill_ComboBox(cmbCategory, "SELECT [Id],(Name + ' - ' + Caption) Name FROM [ptn_HotelCategory] where [IsActive]=1 order by Id")
                Fill_ComboBox(chkRoomCat, "select Id, Name from ptn_RoomCategory where IsActive=1 order by Name")
                Fill_ComboBox(chkRoomType, "SELECT [Id],[Name] from [ptn_RoomType] where [IsActive]=1 order by Id")
                Fill_ComboBox(chkMeal, "SELECT [Id],[Name] from [ptn_MealType] where [IsActive]=1 order by Name")
                If Not Request.QueryString("hid") Is Nothing Then
                    fillData(Request.QueryString("hid"))
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub fillData(ByVal Id As Integer)
        Dim arr As Array
        arr = RetriveData("SELECT [Id], [Name], [CategoryId], [Address], [Phone], [Email], [Person], [City], [Region], [Country], [RoomCategory], [RoomType], [MealType], [Description], [MetaTitle], [MetaKeywords], [MetaDescription], [IsActive], [ViewOrder], [Thumbnail], [GMap], [Supplements] FROM [ptn_Hotels] where Id=" & Id, 21)

        txtName.Text = Server.HtmlDecode(arr(1))
        cmbCategory.SelectedValue = arr(2)
        txtAddress.Text = arr(3)
        txtPhone.Text = arr(4)
        txtEmail.Text = arr(5)
        txtPerson.Text = arr(6)
        txtCity.Text = arr(7)
        txtRegion.Text = arr(8)
        txtCountry.Text = arr(9)

        Select_Checkboxes(chkRoomCat, arr(10), "ptn_RoomCategory")
        Select_Checkboxes(chkRoomType, arr(11), "ptn_RoomType")
        Select_Checkboxes(chkMeal, arr(12), "ptn_MealType")

        txtDescription.Content = Server.HtmlDecode(arr(13))
        txtMTitle.Text = Server.HtmlDecode(arr(14))
        txtMKeywords.Text = Server.HtmlDecode(arr(15))
        txtMDescription.Text = Server.HtmlDecode(arr(16))
        chkIsActive.Checked = arr(17)
        txtViewOrder.Text = arr(18)
        ltrImg.Text = "<img src=""../images/img/" & arr(19) & """ align=""absmiddle"" />"
        ltrMap.Text = "<img src=""../images/maps/" & arr(20) & """ align=""absmiddle"" />"
        'fileGMap.Html = arr(20)
        txtSupp.Content = Server.HtmlDecode(arr(21))


        Dim dt As New DataTable
        dt = Fill_Table("Select Id from ptn_HotelMeal where HotelId=" & Id, "HotelsMeal")

        For Each dr As DataRow In dt.Rows
            For i As Integer = 0 To chkMeal.Items.Count - 1
                If chkMeal.Items(i).Value = dr.Item(0).ToString Then
                    chkMeal.Items(i).Selected = True
                End If
            Next
        Next

    End Sub

    Private Function Get_CheckboxIds(ByVal cmb As Object) As String
        Dim StrIDs As String = ""
        Dim i As Integer
        If cmb.SelectedIndex > -1 Then
            For i = 0 To cmb.Items.Count - 1
                If cmb.Items(i).Selected = True Then
                    If StrIDs.Trim() = "" Then
                        StrIDs = cmb.Items(i).Value
                    Else
                        StrIDs = StrIDs & "," & cmb.Items(i).Value
                    End If
                End If
            Next i
        End If
        Return StrIDs
    End Function

    Private Sub Select_Checkboxes(ByVal cmb As Object, ByVal ids As String, ByVal tablename As String)
        If ids IsNot Nothing Then
            If ids.ToString().Trim() <> "" Then
                Dim dt As New DataTable
                'dt = Fill_Table("select * from mealtype where id in (" & ids & ") order by Name", "Table")
                dt = Fill_Table("select * from " & tablename & " where id in (" & ids & ") order by Name", "Table")

                For Each dr As DataRow In dt.Rows
                    For i As Integer = 0 To cmb.Items.Count - 1
                        If cmb.Items(i).Value = dr.Item(0).ToString Then
                            cmb.Items(i).Selected = True
                        End If
                    Next
                Next
            End If
        End If
    End Sub


    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim intId As Integer
            Dim strActive As String
            Dim strMeal As String = ""
            Dim strRoomType As String = ""
            If chkIsActive.Checked = True Then
                strActive = "1"
            Else
                strActive = "0"
            End If

            If Request.QueryString("hid") Is Nothing Then
                Insert_Update_Delete_Record("INSERT INTO [ptn_Hotels]([Name], [CategoryId], [Address], [Phone], [Email], [Person], [City], [Region], [Country], [RoomCategory], [RoomType], [Description], [MetaTitle], [MetaKeywords], [MetaDescription], [IsActive], [ViewOrder], [Supplements]) VALUES('" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "'," & cmbCategory.SelectedValue & ",'" & Server.HtmlEncode(txtAddress.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPhone.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtEmail.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtPerson.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtCity.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtRegion.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtCountry.Text.Replace("'", "&#039;")) & "','" & Get_CheckboxIds(chkRoomCat) & "','" & Get_CheckboxIds(chkRoomType) & "','" & txtDescription.Content.Replace("'", "&#039;") & "','" & Server.HtmlEncode(txtMTitle.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtMKeywords.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtMDescription.Text.Replace("'", "&#039;")) & "'," & strActive & "," & Server.HtmlEncode(txtViewOrder.Text.Replace("'", "&#039;")) & ",'" & Server.HtmlEncode(txtSupp.Content.Replace("'", "&#039;")) & "')")
                Dim arr As Array
                arr = RetriveData("Select top 1 Id from ptn_Hotels where Name='" & txtName.Text.Replace("'", "&#039;") & "' and MetaTitle='" & txtMTitle.Text.Replace("'", "&#039;") & "' order by Id desc", 0)
                intId = arr(0)
            Else
                Insert_Update_Delete_Record("UPDATE [ptn_Hotels] set [Name]='" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "',[CategoryId]=" & cmbCategory.SelectedValue & ",[Address]='" & Server.HtmlEncode(txtAddress.Text.Replace("'", "&#039;")) & "',[Phone]='" & Server.HtmlEncode(txtPhone.Text.Replace("'", "&#039;")) & "',[Email]='" & Server.HtmlEncode(txtEmail.Text.Replace("'", "&#039;")) & "',[Person]='" & Server.HtmlEncode(txtPerson.Text.Replace("'", "&#039;")) & "',[City]='" & Server.HtmlEncode(txtCity.Text.Replace("'", "&#039;")) & "',[Region]='" & Server.HtmlEncode(txtRegion.Text.Replace("'", "&#039;")) & "',[Country]='" & Server.HtmlEncode(txtCountry.Text.Replace("'", "&#039;")) & "',[RoomCategory]='" & Get_CheckboxIds(chkRoomCat) & "',[RoomType]='" & Get_CheckboxIds(chkRoomType) & "',[Description]='" & txtDescription.Content.Replace("'", "&#039;") & "',[MetaTitle]='" & Server.HtmlEncode(txtMTitle.Text.Replace("'", "&#039;")) & "',[MetaKeywords]='" & Server.HtmlEncode(txtMKeywords.Text.Replace("'", "&#039;")) & "',[MetaDescription]='" & Server.HtmlEncode(txtMDescription.Text.Replace("'", "&#039;")) & "',[IsActive]=" & strActive & ",[ViewOrder]=" & Server.HtmlEncode(txtViewOrder.Text.Replace("'", "&#039;")) & ",[Supplements]='" & Server.HtmlEncode(txtSupp.Content.Replace("'", "&#039;")) & "' WHERE Id=" & Request.QueryString("hid"))
                intId = Request.QueryString("hid")
            End If

            If fileUpload.FileName.Length > 0 Then
                UploadFile("ptn_Hotels", "Thumbnail", fileUpload, intId, "/img")
            End If

            If fileGMap.FileName.Length > 0 Then
                UploadFile("ptn_Hotels", "GMap", fileGMap, intId, "/maps")
            End If



            Insert_Update_Delete_Record("delete from ptn_HotelMeal where HotelId=" & intId)
            If chkMeal.SelectedIndex > -1 Then
                For i As Integer = 0 To chkMeal.Items.Count - 1
                    If chkMeal.Items(i).Selected = True Then
                        Insert_Update_Delete_Record("insert into ptn_HotelMeal(HotelId,MealId) values (" & intId & "," & chkMeal.Items(i).Value & ")")
                    End If
                Next i
            End If

            Response.Redirect("ViewHotels.aspx")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class